
<?php 
	$dialogId = "DialogAddQuestion";
	$messageErrorId = "messageErrorId";
?>

<div class="box_mid" id="<?= $dialogId?>" title="Add question">
	<?php echo $this->Session->flash(); ?>
	<?php echo "<div id='$messageErrorId'></div>";?>
	<div class="box_mid_content">
		<div class="box_mid_content_top">
		</div>
		<div class="box_mid_content_mid">
		<?php $idCkeditor = 'Content'.$parent_id ;?>
		<?= $this->Form->create('Question')?>
			<div class="info">
				<?= $this->Form->input('question_type_id',
						array('div'=>false, 'label'=>'Question type', 'type'=> 'select', 'id'=>'questionType'.$parent_id, 'options'=>$QuestionTypes))?>
			</div>
			<div class="info">
				<?= $this->Form->input('question_level_id',
						array('div'=>false, 'label'=>'Question level', 'type'=> 'select', 'id'=>'questionLevel'.$parent_id, 'options'=>$QuestionLevels)); ?>
			</div>
			<?= $this->Form->input('content', array('id'=>$idCkeditor));?>
			<?= $this->CKEditor->load($idCkeditor) ?>
			<?= $this->Form->end(); ?>
		</div>
	</div>
	<div id="temp"></div>
</div>

<script type="text/javascript">
	function ShowQuestionById(question_id, div_id) {
		$.ajax({
			async:true,
			url: "../Questions/Show/"+question_id+"/<?= $position ?>",
			beforeSend:function(request){

			},
			complete:function(request, json){
				html = "<div class='clear'></div>";
				html += '<li>'+request.responseText+'</li>';
				$('#'+div_id).append(html);
			}  
		});
	}
	$(function() {	
		$( "#<?= $dialogId ?>" ).dialog( "destroy" );
		var questionType = $("#questionType" + "<?= $parent_id?>"),
		questionLevel = $( "#questionLevel" + "<?= $parent_id ?>" );
		
		$("#<?= $dialogId ?>").dialog({
			autoOpen: true,
			modal: true,
			bgiframe: true,
			height: 600,
			width: 1000,
			title: "Add question" ,
			show: { effect: 'drop', direction: "up" },
			hide: { effect: 'drop', direction: "down" },
			buttons: [
						{
						    text: "Save",
						    click: function(){
						    	var questionContent_data = CKEDITOR.instances["<?= $idCkeditor?>"].getData();
								$.ajax({
									async:true,
									dataType: "Json",
									url: "/eonline/questions/AddQuestionByJson",
									data: {
										'parent_id' : <?= $parent_id ?>,
										'question_type_id': questionType.val(),
										'question_level_id': questionLevel.val(),
										'content': questionContent_data
									},
									beforeSend:function(request){
										
									},
									complete:function(request, json){
										if(json == 'success'){
											var dataResult = JSON.parse(request.responseText);

											if (dataResult['status']) {
												$('#<?= $dialogId ?>').dialog("close");
												ShowQuestionById(dataResult['questionId'], 'container-question-child');
											}else{
												var messages = "<div class='error-message'>"+dataResult['message']+"</div>";
												$('#<?= $messageErrorId ?>').html(messages);
											}
										}
									}  
								});
						    }
						 },
						{
							text: "Close",
							click: function(){
								$(this).dialog("close");
							}
						}
					],
				close: function(){
					$('#<?= $dialogId ?>').remove();
					$('.ui-effects-wrapper').remove();
				}
		});
	});
</script>